/*
2011-08-05
 1. MyWebORUtility.checkDoor 增加變數判斷 $context_path$。

2011-05-20
 1. for debug 需求以 log4j 管理加入 DeBuger，由 DeBuger 整合控管訊息列印。

*/

package idv.jieh.web;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import idv.jieh.web.html.SimplePage;
import idv.jieh.commons.util.DataAnalysis;

public abstract class StrutsCtrl extends Action implements java.io.Serializable {

  private boolean disable_security = false;

  private idv.jieh.commons.util.DeBuger dbr;

  public StrutsCtrl(){
    dbr = new idv.jieh.commons.util.DeBuger(StrutsCtrl.class.getName());
  }

  public void disableSecurity(){
    disable_security = true;
  }

  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
  {
    ActionMessages messages = new ActionMessages();
    ActionForward actionForward = null;

    try {
      if(disable_security || checkDoor(servlet.getServletContext(), request, response)) {
        actionForward = process(mapping, form, request, response, messages);
      }
    } catch (Exception e) {
      dbr.exception(e);
//      e.printStackTrace();
    }

    saveToken(request);

    saveErrors(request, messages);
    String np = mapping.getInput();
    if (np != null) actionForward = new ActionForward(np);

    return actionForward;
  }


  protected boolean checkDoor(
    ServletContext sc,
    HttpServletRequest rq,
    HttpServletResponse rp)
  throws java.io.IOException{
    return new MyWebORUtility().checkDoor(sc, rq, rp);
  }


  protected boolean want(HttpServletRequest request, SimplePage.BTN ask) {
    return request.getParameter(ask.name()) != null;
  }

  protected boolean isPageID(HttpServletRequest request, Object pageid) {
    String request_pageid = request.getParameter(SimplePage.PAGEID_KEY);
    return pageid == null || request_pageid == null
      ?	false
      :	request_pageid.equals(pageid.toString());
  }

  protected boolean isSelectionSubmit(HttpServletRequest request){
//    String flag = request.getParameter(new com.tcbank.commons.web.SelectionPlus().getFlagName());
    String flag = request.getParameter("");
    return flag != null && flag.equals("1");
  }

  protected abstract ActionForward process(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response,
    ActionMessages messages) throws Exception;

}

class MyWebORUtility{

  private final static String VAL_CONTEXT_PATH = "$context_path$";

  boolean checkDoor(
    ServletContext sc,
    HttpServletRequest rq,
    HttpServletResponse rp)
  throws java.io.IOException{
    String session_name = sc.getInitParameter(OnlineResource.USERONLINE_SESSION_NAME);
    OnlineCheckDoor checkdoor = (OnlineCheckDoor)rq.getSession(true).getAttribute(session_name);

    if (checkdoor == null || (!checkdoor.isLogin8Cookie(rq) && !checkdoor.isLogin())) {
      // 2011-08-05
      String checkdoor_path = sc.getInitParameter(OnlineCheckDoor.CHECKDOOR_PATH);
      if(checkdoor_path.indexOf(VAL_CONTEXT_PATH) != -1){
        String context_path = rq.getContextPath();
        checkdoor_path = DataAnalysis.replace(checkdoor_path, VAL_CONTEXT_PATH, context_path);
      }
      rp.sendRedirect(checkdoor_path);
      // 2011-08-05
      return false;
    }
    return true;
  }
}
